﻿using System;
using System.Collections.Generic;
using System.Text;

using Torch.WL.DataAccess.CleanChina;
using Torch.WL.Entity.CleanChina;
using Torch.FrameWork.Utility;

namespace Torch.WL.Business.CleanChina
{
    class TbNewsManager
    {
        private readonly TbNewsAccess dal = new TbNewsAccess();

        private static TbNewsManager _newsManager= new TbNewsManager ();
        public static TbNewsManager Instance()
        {
            return _newsManager;
        }

        private TbNewsManager()
        {         
        }

        public TbNewsEntity GetModel(int newsId)
        {
            return dal.GetModel(newsId);
        }

        public List<TbNewsEntity> GetNewsList(int lessthan_newsid, int pagesize)
        {
            if (lessthan_newsid == -1) lessthan_newsid = Int32.MaxValue;
            if (pagesize == 0) pagesize = 10; 

            return dal.GetNewsList(lessthan_newsid, pagesize);
        }

        public TbNewsEntity GetModelByCache(int newsId)
        {

            string CacheKey = "TbNewsEntityModel-" + newsId;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(newsId);
                    if (objModel != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("EntityCacheMinutes");
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (TbNewsEntity)objModel;
        }


    }
}
